package com.xwltz.core.exception;

import com.xwltz.core.constants.HttpStatus;
import com.xwltz.core.domain.ActionResult;
import com.xwltz.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @description 全局异常处理器
 * @information on version 1.0 be created by xwltz at 2021/12/29 15:03
 */

@Slf4j
@RestControllerAdvice
public class FrameworkException {

	/**
	 * 权限校验异常
	 */
	@ExceptionHandler(AccessDeniedException.class)
	public ActionResult<Void> handleAccessDeniedException(AccessDeniedException e, HttpServletRequest request) {
		String requestURI = request.getRequestURI();
		log.error("请求地址'{}',权限校验失败'{}'", requestURI, e.getMessage());
		return ActionResult.error(HttpStatus.HTTP_FORBIDDEN, "没有权限，请联系管理员授权");
	}

	/**
	 * 请求方式不支持
	 */
	@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
	public ActionResult<Void> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) {
		String requestURI = request.getRequestURI();
		log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
		return ActionResult.error(e.getMessage());
	}

	/**
	 * 业务异常
	 */
	@ExceptionHandler(ServiceException.class)
	public ActionResult<Void> handleServiceException(ServiceException e, HttpServletRequest request) {
		String requestURI = request.getRequestURI();
		log.error("请求地址'{}', 异常信息'{}', Exception:'{}'", requestURI, e.getMessage(), e);
		Integer code = e.code;
		return StringUtils.isNotNull(code) ? ActionResult.error(code, e.getMessage()) : ActionResult.error(e.getMessage());
	}

	/**
	 * 拦截未知的运行时异常
	 */
	@ExceptionHandler(RuntimeException.class)
	public ActionResult<Void> handleRuntimeException(RuntimeException e, HttpServletRequest request) {
		String requestURI = request.getRequestURI();
		log.error("请求地址'{}',发生未知异常.", requestURI, e);
		return ActionResult.error(e.getMessage());
	}

	/**
	 * 系统异常
	 */
	@ExceptionHandler(Exception.class)
	public ActionResult<Void> handleException(Exception e, HttpServletRequest request) {
		String requestURI = request.getRequestURI();
		log.error("请求地址'{}',发生系统异常.", requestURI, e);
		return ActionResult.error(e.getMessage());
	}

	/**
	 * 自定义验证异常
	 */
	@ExceptionHandler(BindException.class)
	public ActionResult<Void> handleBindException(BindException e) {
		log.error(e.getMessage(), e);
		String message = e.getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining(", "));
		return ActionResult.error(message);
	}

	/**
	 * 自定义验证异常
	 */
	@ExceptionHandler(ConstraintViolationException.class)
	public ActionResult<Void> constraintViolationException(ConstraintViolationException e) {
		log.error(e.getMessage(), e);
		String message = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(", "));
		return ActionResult.error(message);
	}

	/**
	 * 自定义验证异常
	 */
	@ExceptionHandler(MethodArgumentNotValidException.class)
	public ActionResult<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
		log.error(e.getMessage(), e);
		String message = Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage();
		return ActionResult.error(message);
	}
}
